﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Windows;
using System.Windows.Threading;
using System.Windows.Xps.Packaging;
using CodeReason.Reports;
using System.Diagnostics;
using report_demo.Data;

namespace report_demo {
    /// <summary>
    /// Interaction logic for DynamicReportDemo.xaml
    /// </summary>
    public partial class DynamicReportDemo : Window {
        private ReportData _reportData;
        public DynamicReportDemo() {
            InitializeComponent();
        }

        public static void show() {
            DynamicReportDemo form = new DynamicReportDemo();
            form.Show();
        }
        /// <summary>
        /// 
        /// </summary>
        private void initData() {
            String[] cols = CustomerData.getInstance().getColumnNameArray();
            object[][][] values = CustomerData.getInstance().getComplexData();
            int[] prices = new int[] { 0, 0, 0, 0 };
            //
            DataTable table = new DataTable("FOOD");
            table.Columns.Add(cols[0], typeof(string));
            table.Columns.Add(cols[1], typeof(string));
            table.Columns.Add(cols[2], typeof(int));
            table.Columns.Add(cols[3], typeof(string));
            for (int i = 0; i < 20; i++) {
                table.Rows.Add(values[0][i]);
                prices[0] += (int)values[0][i][2];
            }
            _reportData.DataTables.Add(table);

            //
            table = new DataTable("TV");
            table.Columns.Add(cols[0], typeof(string));
            table.Columns.Add(cols[1], typeof(string));
            table.Columns.Add(cols[2], typeof(int));
            table.Columns.Add(cols[3], typeof(string));
            for (int i = 0; i < 20; i++) {
                table.Rows.Add(values[1][i]);
                prices[1] += (int)values[1][i][2];
            }
            _reportData.DataTables.Add(table);
            //
            table = new DataTable("PHONE");
            table.Columns.Add(cols[0], typeof(string));
            table.Columns.Add(cols[1], typeof(string));
            table.Columns.Add(cols[2], typeof(int));
            table.Columns.Add(cols[3], typeof(string));
            for (int i = 0; i < 20; i++) {
                table.Rows.Add(values[2][i]);
                prices[2] += (int)values[2][i][2];
            }
            _reportData.DataTables.Add(table);
            //
            table = new DataTable("AUDIO");
            table.Columns.Add(cols[0], typeof(string));
            table.Columns.Add(cols[1], typeof(string));
            table.Columns.Add(cols[2], typeof(int));
            table.Columns.Add(cols[3], typeof(string));
            for (int i = 0; i < 20; i++) {
                table.Rows.Add(values[3][i]);
                prices[3] += (int)values[3][i][2];
            }
            _reportData.DataTables.Add(table);
            //
            for (int i = 0; i < 4; i++) {
                _reportData.ReportDocumentValues.Add("sub_total_" + i, prices[i]);
            }



        }
        /// <summary>
        /// 
        /// </summary>
        private void CreateXpsDocument() {

            Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(delegate {
                try {
                    ReportDocument reportDocument = new ReportDocument();

                    StreamReader reader = new StreamReader(new FileStream(@"Templates\DynamicReport.xaml", FileMode.Open, FileAccess.Read));
                    reportDocument.XamlData = reader.ReadToEnd();
                    reportDocument.XamlImagePath = Path.Combine(Environment.CurrentDirectory, @"Templates\");
                    reader.Close();
                    _reportData = new ReportData();
                    // set constant document values
                    _reportData.ReportDocumentValues.Add("PrintDate", DateTime.Now); // print date is now
                    //


                    DataTable dataTable = new DataTable();
                    

















                    
                    //
                    DateTime dateTimeStart = DateTime.Now; // start time measure here
                    XpsDocument xps = reportDocument.CreateXpsDocument(_reportData);

                    // concat XPS files
                    documentViewer.Document = xps.GetFixedDocumentSequence();

                    // show the elapsed time in window title
                    Title += " - generated in " + (DateTime.Now - dateTimeStart).TotalMilliseconds + "ms";
                } catch (Exception ex) {
                    // show exception
                    MessageBox.Show(ex.Message + "\r\n\r\n" + ex.GetType() + "\r\n" + ex.StackTrace, ex.GetType().ToString(), MessageBoxButton.OK, MessageBoxImage.Stop);
                } finally {
                    //busyDecorator.IsBusyIndicatorHidden = true;
                }
            }));
        }


    }
}
